package com.jlect.swebing.ui.client.button;

import com.jlect.swebing.ui.client.ActionEvent;
import com.jlect.swebing.ui.client.ActionListener;

/**
 * Default button view implementation.
 *
 * @author Sergey Kozmin
 * @since 23.11.2007 9:45:23
 */
public class ToggleButtonControllerImpl implements ButtonController, ActionListener {
    private ButtonView view;
    private ButtonModel model;

    public void setModel(ButtonModel model) {
        if(model == null) {
            throw new IllegalArgumentException("Model object could not be null. ");
        }
        this.model = model;
    }

    public void setView(ButtonView view) {
        if(view == null) {
            throw new IllegalArgumentException("View object could not be null. ");
        }
        this.view = view;
        view.addActionListener(this);
    }

    public void addActionListener(ActionListener listener) {
        view.addActionListener(listener);
    }

    public void removeActionListener(ActionListener listener) {
        view.removeActionListener(listener);
    }

    public void actionPerformed(ActionEvent event) {
        model.setPressed(!model.isPressed());
        model.setSelected(!model.isPressed());
    }
}